#include"XYXKC_I2C.h"
XYXKC_I2C::XYXKC_I2C(){}

/**
* @brief 设备以主机的形式加入
*/
void XYXKC_I2C::init(){
    _wire->begin();
}

/**
* @brief 初始化 设置从机的地址
*/
void XYXKC_I2C::begin()
{
    _i2caddr = I2C_STC8H8K64U_ADDR;
    _wire = &Wire;
    init();
}

/**
 *  @brief  获取颜色传感器数据
 *  @param  date 数据存放地址
 */
void XYXKC_I2C::read(uint8_t *date)
{
    //发送读取停止，避免发送的数据被读出去，导致发送失败 
    _wire->requestFrom(_i2caddr,(uint8_t)1,true);
    // while (_wire->available()> 0) 
    // { 
    //     #if ARDUINO >= 100
    //         _wire->read();
    //     #else
    //        _wire->receive();
    //     #endif  
    // }
    int i = 0;
    _wire->requestFrom(_i2caddr, (uint8_t)16);    
    while (_wire->available()> 0) 
    { 
        #if ARDUINO >= 100
            date[i] = _wire->read();
        #else
            date[i] = _wire->receive();
        #endif
        i++;    
    }
}

/**
 *  @brief  发送一个字节数据
 *  @param  sentDate 发送的数据 senDate 范围0~256 
 */
void XYXKC_I2C::write(uint8_t sentDate)
{

    _wire->beginTransmission(_i2caddr);
    #if ARDUINO >= 100
    _wire->write(0);
    _wire->write(sentDate);
    #else
    _wire->send(0);
    _wire->send(sentDate);
    #endif
    _wire->endTransmission();
    

}
/**
*  @brief 获取状态字符串
*/
char* XYXKC_I2C::readString(uint8_t len){
    if(len == 1){
        for (int i = 8; i < 16; i++) {
            _charData1[i-8] = date[i] + '0';
        } 
        _charData1[8] = '\0';
        return _charData1;
    }else if(len == 2) {
        for (int i = 0; i < 8; i++) {
            _charData1[i] = date[i] + '0';
        } 
        _charData1[8] = '\0';
        return _charData1;
    }else{
        for (int i = 8; i < 16; i++) {
            _charData2[i-8] = date[i] + '0';
        }
        for (int i = 0; i < 8; i++) {
            _charData2[i+8] = date[i] + '0';
        }  
        _charData2[16] = '\0';
        return _charData2;
    }

}
